/*
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.chimos.property.compiler.passone;


import static org.objectweb.asm.Opcodes.ACC_FINAL;
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
import static org.objectweb.asm.Opcodes.ACC_PROTECTED;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ASM4;

import java.util.List;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;

import de.chimos.property.compiler.Config;
import de.chimos.property.compiler.PropertyInfo;

/**
 * @author Niklas Hofmann
 */
public class PassOneClassVisitor extends ClassVisitor {

    private String className;
    
    private final List<PropertyInfo> properties;
    
    private final Config config;
    
    public PassOneClassVisitor(List<PropertyInfo> properties, Config config)
    {
        super(ASM4);
        
        this.properties = properties;
        
        this.config = config;
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        className = name;
    }

    @Override
    public FieldVisitor visitField(int access, String name, String desc, String signature, Object value)
    {
        if(desc.startsWith("L") && (access == ACC_PUBLIC || access == ACC_PROTECTED || access == ACC_PRIVATE))
        {
            return new PassOneFieldVisitor(className, name, desc, signature, access, properties, config);
        }
        
        return null;
    }

	@Override
	public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions)
	{
		if(name.endsWith("Property") && (access == (ACC_PUBLIC+ACC_FINAL) || access == (ACC_PROTECTED+ACC_FINAL) || access == (ACC_PRIVATE+ACC_FINAL)))
		{
			return new PassOneMethodVisitor(className, properties, config);
		}
		
		return null;
	}
    
}
